import 'dart:convert';
import 'package:dali_ng_pera/manager/dnp_plugin_manager.dart';
import 'package:dali_ng_pera/net/dnp_net_manager.dart';
import 'package:dio/dio.dart' as dio;
import 'package:geolocator/geolocator.dart';

import '../tool/dnp_location_tool.dart';

class DnpMob {

   static indescribable() async {

     dynamic indescribable = await DnpPluginManager().plugin.indescribable;

     Map<String, dynamic> indescribableMap = Map<String, dynamic>.from(indescribable);

     var result = await DnpNetManager().post(
       '/haseye/indescribable',
       data: dio.FormData.fromMap(indescribableMap)
     );

     if (result.success) {}
   }

   static fervour() async {
     String handled = await DnpPluginManager().plugin.footed ?? "";
     String enterprise = await DnpPluginManager().plugin.fearlessness ?? "";

     var result = await DnpNetManager().post(
         '/haseye/fervour',
         data: dio.FormData.fromMap({
           "handled" : handled,
           "enterprise" : enterprise,
         })
     );

     if (result.success) {}
   }

   static inexplicable(
       String nervous,
       int began,
       int gone,
       {String? paid}
    ) async {

     String possession = await DnpPluginManager().plugin.footed ?? "";
     String concerned = await DnpPluginManager().plugin.fearlessness ?? "";

     // Map<dynamic,dynamic>? locationInfo = await DnpPluginManager().plugin.startLocationTracking;
     //
     // String carrying = locationInfo?["longitude"];
     // String openly = locationInfo?["latitude"];

     String carrying = "";
     String openly = "";

     bool hasPermission = await DnpLocationTool.checkLocationPermission();

     if (hasPermission) {
       Position position = await DnpLocationTool.getFreshLocation();
       carrying = position.longitude.toStringAsFixed(6);
       openly = position.latitude.toStringAsFixed(6);
     }

     Map<String, dynamic> params = {
       "nervous" : nervous,
       "naked" : "2",
       "possession" : possession,
       "concerned" : concerned,
       "carrying" : carrying,
       "openly" : openly,
       "began" : began.toString(),
       "gone" : gone.toString(),
       "paid" : paid ?? ""
     };

     var result = await DnpNetManager().post(
         '/haseye/inexplicable',
         data: dio.FormData.fromMap(params)
     );

     DnpPluginManager().plugin.endLocationTracking();

     if (result.success) {
        print("数字:${nervous} === 经度:${carrying} === 纬度:${openly} === 开始时间:${began} === 结束时间:${gone} === 缴费:${paid}");
     }
   }

   static within() async {

     Map<dynamic,dynamic>? treasures = await DnpPluginManager().plugin.treasures;

     Map<dynamic,dynamic>? wire = await DnpPluginManager().plugin.wire;

     Map<dynamic,dynamic> less = {
       "considerably" : await DnpPluginManager().plugin.considerably,
       "afford" : await DnpPluginManager().plugin.afford,
       "cannot" : await DnpPluginManager().plugin.cannot,
     };

     Map<dynamic,dynamic> shillings = {
       "ten" : await DnpPluginManager().plugin.ten,
       "should" : await DnpPluginManager().plugin.should,
     };

     Map<dynamic,dynamic> tray = {
       "largest" : await DnpPluginManager().plugin.largest,
       "handled" : await DnpPluginManager().plugin.footed,
       "autou" : await DnpPluginManager().plugin.autou,
       "finger" : await DnpPluginManager().plugin.finger,
       "enterprise" : await DnpPluginManager().plugin.fearlessness,
     };

     Map<dynamic,dynamic>? shook = await DnpPluginManager().plugin.shook;

     Map closeMap = {
       "treasures" : treasures,
       "wire" : wire,
       "less" : less,
       "shillings" : shillings,
       "tray" : tray,
       "shook" : shook
     };

     String closeJsonString = jsonEncode(closeMap);

     var result = await DnpNetManager().post(
         '/haseye/within',
         data: dio.FormData.fromMap({
           "close" : closeJsonString
         })
     );

     if (result.success) {}

   }

   static night() async {

     var night = await DnpPluginManager().plugin.chooseContactList;

     String closeJsonString = jsonEncode(night);

     var result = await DnpNetManager().post(
         '/haseye/night',
         data: dio.FormData.fromMap({
           "close" : closeJsonString
         })
     );

     if (result.success) {}
   }
}